﻿using Max.Ph5_2009A.NewShopping.EFCore;
using Max.Ph5_2009A.NewShopping.IRespository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Max.Ph5_2009A.NewShopping.Respository
{
    /// <summary>
    /// 实现基本的仓储功能（CRUD功能）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRespository<T> : IBaseRespository<T> where T : class,new()
    {
        /// <summary>
        /// 通过构造函数注入上下文对象
        /// </summary>
        PH52009ANewShoppingDbContext _context;

        public BaseRespository(PH52009ANewShoppingDbContext context)
        {
            _context = context;
        }

        #region 同步方法


        /// <summary>
        /// 删除数据（单删除或批量删除）
        /// </summary>
        /// <param name="t"></param>
        public void Delete(List<T> t)
        {
           _context.Set<T>().RemoveRange(t);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        public void Insert(T t)
        {
            _context.Set<T>().Add(t);
        }


        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Select()
        {
            return _context.Set<T>();
        }


        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="t"></param>
        public void Update(T t)
        {
            _context.Set<T>().Update(t);
        }


        #endregion

        #region 异步方法

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        public void InsertAsync(T t)
        {
            _context.Set<T>().AddAsync(t);
        }

        public void DeleteAsync(List<T> t)
        {
            _context.Set<T>().RemoveRange(t);
        }

        public void UpdateAsync(T t)
        {
            _context.Set<T>().Update(t);
        }

        public IQueryable<T> SelectAsync()
        {
            return _context.Set<T>().AsQueryable();
        }
        #endregion
    }
}
